package cn.erperp.cdc.maxwell.controller;


import cn.erperp.cdc.maxwell.dao.BusEventsApiEntityDao;
import cn.erperp.cdc.maxwell.entity.BusEventsApiEntity;
import cn.erperp.cdc.maxwell.msg.MQConfig;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.Instant;

@RestController
public class DemoController{

    @Autowired
    BusEventsApiEntityDao busEventsApiEntityDao;

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 模拟发送消息
     */
    @SneakyThrows
    @RequestMapping("mockMessage")
    public void mockMessage(@RequestParam("count") int count) {
        for (int i = 0; i < count; i++) {
            BusEventsApiEntity obj = new BusEventsApiEntity();
            obj.setId(IdWorker.getId());
            obj.setClassName("haha");
            obj.setEventJson("{\"key\":\"value\",\"messageId\":\"" + obj.getId() + "\"}");
            obj.setCreatedDate(Timestamp.from(Instant.now()));
            obj.setCreatingOwner("maxwell-self");
            obj.setMqType("mns");
            MQConfig mqConfig = new MQConfig();
            mqConfig.setQueueName("maxwell");
            obj.setMqJson(objectMapper.writeValueAsString(mqConfig));
            busEventsApiEntityDao.save(obj);
        }
    }
}
